package order.servlet;
import order.dao.loginDao;
import order.pojo.admin;
import order.pojo.customer;
import order.pojo.manager;
import order.utils.DbUtil;

import javax.servlet.ServletException;
import  javax.servlet.http.*;
import java.io.IOException;
import java.sql.Connection;

public class loginServlet extends HttpServlet {
    DbUtil dbUtil = new DbUtil();
    loginDao loginDao =new loginDao();



    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //获取login.jsp提交表单中的字段并存入session中
//        HttpSession session=req.getSession();
        String userName = req.getParameter("userName");
        String password = req.getParameter("password");
        String userType = req.getParameter("userType");

        Connection con=null;
        try{

            con=dbUtil.getCon();

            order.pojo.admin currentAdmin=null;//用来判断的currentAdmin对象
            manager currentMannager=null;
            customer currentCustomer=null;
            if(userType.equals("admin")){//admin验证

            admin admin=new admin(userName,password);
            currentAdmin= loginDao.selectAdminBtId(con);

            boolean flag=admin.getAdmin_id().equals(currentAdmin.getAdmin_id())&&admin.getAdmin_password().equals(currentAdmin.getAdmin_password());

            if(flag){

//                session.setAttribute("currentUserType","admin");
//                session.setAttribute("currentUser",currentAdmin);

                req.setAttribute("mainPage", "main/index.jsp");
                req.getRequestDispatcher("main/index.jsp").forward(req,resp);

            }else{     //判断下查出来的值是否为空，再重定位到首页

                req.setAttribute("admin",admin);
                req.setAttribute("error","用户名或密码错误！");
                req.getRequestDispatcher("").forward(req,resp);


            }



            }else if ("manager".equals(userType)){//店家验证

                manager manager=new manager(null,userName,password);
               currentMannager= loginDao.managerLogin(con);
               boolean flag=currentMannager.getManager_account().equals(manager.getManager_account()) && currentMannager.getManager_password().equals(manager.getManager_password());

                if(currentMannager.getManager_account().equals(manager.getManager_account()) && currentMannager.getManager_password().equals(manager.getManager_password())){


//                    session.setAttribute("currentUserType","manager");
//                    session.setAttribute("currentUser",currentAdmin);
                    req.setAttribute("mainPage", "main/manage.jsp");

                    req.getRequestDispatcher("main/manage.jsp").forward(req,resp);
                }else{
                    //判断下查出来的值是否为空，再重定位到首页

                    req.setAttribute("manager",manager);
                    req.setAttribute("error","用户名或密码错误！");

                    req.getRequestDispatcher("/").forward(req,resp);

                }





            }else if ("customer".equals(userType)){//顾客登陆验证
                customer customer=new customer((Integer) null,userName,password);
                currentCustomer= loginDao.customerLogin(con);
                boolean flag=currentCustomer.getCustomer_account().equals(customer.getCustomer_account())&&currentCustomer.getCustomer_password().equals(customer.getCustomer_password());

                if(flag){
//                    session.setAttribute("currentUserType","customer");
//                    session.setAttribute("currentUser",  currentCustomer);

                    req.setAttribute("mainPage", "main/customer.jsp");
                    req.getRequestDispatcher("main/customer.jsp").forward(req,resp);

                }else{
//                   //判断下查出来的值是否为空，再重定位到首页
//
                    req.setAttribute("customer",customer);
                    req.setAttribute("error","用户名或密码错误！");
                    req.getRequestDispatcher("/").forward(req,resp);

                }

            }






        }catch (Exception E){

        }finally {
            try{
                dbUtil.closeCon(con);
            }catch (Exception e){
                e.printStackTrace();
            }

        }











    }
    private void deleteCookie(String userName, HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies=request.getCookies();
        for(int i=0;cookies!=null && i<cookies.length;i++){
            if(cookies[i].getName().equals("dormuser")){
                if(userName.equals(userName=cookies[i].getValue().split("-")[0])) {
                    Cookie cookie = new Cookie(cookies[i].getName(), null);
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                    break;
                }
            }
        }
    }
}
